<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>权限管理</title>
</head>
<style>
    .wu-pos-a {
        box-sizing: border-box;
        padding-top: 39px;
        width: 20%;
        left: auto;
        right: 0;
    }
</style>
<body>
<div class="pos-r">
    <nav class="breadcrumb">
        <i class="Hui-iconfont">&#xe67f;</i> 首页
        <span class="c-gray en">&gt;</span> 系统管理
        <span class="c-gray en">&gt;</span> 权限管理
    </nav>
    <div class="pos-a wu-pos-a" >
        <div class="wu-tree">角色树</div>
        <div class="wu-treeBox">
            <ul id="merchant-systemAuthorityTree" class="ztree"></ul>
        </div>

    </div>
    <div style="margin-right:22%">
        <div class="page-container">
            <div class="row">
                <div class="col-xs-12">
                    <table id="merchant-bizAuthorityTable" class="table table-bordered table-striped text-center">
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<!--修改的模态框-->
<div class="modal fade merchant-system-authority-edit-modal" tabindex="-1" role="dialog">
    <input id="merchant-authorityIndexId" value="" style="display: none;"/>
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">修改权限</h4>
            </div>
            <div class="modal-body">
                <form>
                    <ul id="merchant-systemAuthorityTree2" class="ztree"></ul>
                </form>
            </div>
            <div class="modal-footer">
                <button onclick="merchantSystemAuthorityFun.subBizUserRole();" type="button" class="btn btn-primary">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<script>
    //平台的权限管理
    var merchantSystemAuthorityFun;
    merchantSystemAuthorityFun = {
        init : function () {
            //加载右侧角色树图
            $.ajax({
                type: "GET",
                url: "menu/selectBizRole",
                dataType: "json",
                success: function(data){
                    var setting = {
                        check: {
                            enable: true
                        },
                        data: {
                            simpleData: {
                                enable: true
                            }
                        }
                    };
                    $.fn.zTree.init($("#merchant-systemAuthorityTree"), setting, data);
                    var systemAuthorityTreeObj = $.fn.zTree.getZTreeObj("systemAuthorityTree");
//                    systemAuthorityTreeObj.expandAll(true);
                    /**************测试的修改ztree*****/
                    $.fn.zTree.init($("#merchant-systemAuthorityTree2"), setting, data);
                    var systemAuthorityTree2Obj = $.fn.zTree.getZTreeObj("systemAuthorityTree2");
//                    systemAuthorityTree2Obj.expandAll(true);

                },
                error:function () {
                    alert("error");
                }
            });
            //加载表格
            $('#merchant-bizAuthorityTable').bootstrapTable({
                url: 'user/getAllBizUserByBizId',         //请求后台的URL（*）
                method: 'get',
                striped: true,                      //是否显示行间隔色
                cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                pagination: true,                   //是否显示分页（*）
                sortable: false,                     //是否启用排序
                sortOrder: "asc",                   //排序方式
                sidePagination: "client",           //分页方式：client客户端分页，server服务端分页（*）
                pageNumber: 1,                       //初始化加载第一页，默认第一页
                pageSize: 10,                       //每页的记录行数（*）
                pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
                strictSearch: true,
                showColumns: true,                  //是否显示所有的列
                showRefresh: true,                  //是否显示刷新按钮
                minimumCountColumns: 2,             //最少允许的列数
                clickToSelect: true,                //是否启用点击选中行
                uniqueId: "ID",                     //每一行的唯一标识，一般为主键列
                showToggle: true,                    //是否显示详细视图和列表视图的切换按钮
                cardView: false,                    //是否显示详细视图
                detailView: false,                   //是否显示父子表
                onClickRow: function (row) {
                    //单击行事件，获取整行信息row
                    $.ajax({
                        type: "GET",
                        url: "user/getByUserId?userId="+row.userId,
                        dataType: "json",
                        success: function(data){
                            //首先取消所有选中状态
                            var treeObj = $.fn.zTree.getZTreeObj("systemAuthorityTree");
                            treeObj.checkAllNodes(false);
                            treeObj.cancelSelectedNode();

                            //获取所有节点的信息data
                            for(var i =0;i<data.length;i++){
                                var zTree = $.fn.zTree.getZTreeObj("systemAuthorityTree");
                                var node = zTree.getNodeByParam("id",data[i].roleId);
                                node.checked = true;
                                //设置选中之后，需要更新一次，否则无法选中该节点
                                zTree.updateNode(node);
                            }

                            //模态框的tree
                            var treeObj2 = $.fn.zTree.getZTreeObj("systemAuthorityTree2");
                            treeObj2.checkAllNodes(false);
                            treeObj2.cancelSelectedNode();

                            //获取所有节点的信息data
                            for(var i =0;i<data.length;i++){
                                var zTree2 = $.fn.zTree.getZTreeObj("systemAuthorityTree2");
                                var node2 = zTree2.getNodeByParam("id",data[i].roleId);
                                node2.checked = true;
                                //设置选中之后，需要更新一次，否则无法选中该节点
                                zTree2.updateNode(node2);
                            }

                        },
                        error:function () {
                            alert("error");
                        }
                    });
                },
                columns: [
                    {
                        field: 'userId',
                        title: '角色id',
                        visible:false
                    }, {
                        field: 'realName',
                        title: '真实姓名'
                    }, {
                        field: 'sex',
                        title: '性别',
                        formatter:function(value,row,index){
                            if(value == '0'){
                                return '女';
                            }else if(value == '1') {
                                return '男';
                            }else {
                                return '未知';
                            }
                        }
                    },{
                        field: 'state',
                        title: '状态',
                        formatter:function(value,row,index){
                            if(value == '1'){
                                return '在职';
                            }else if(value == '0') {
                                return '离职';
                            }else if(value == '2') {
                                return '禁用';
                            }
                        }
                    }, {
                        field: 'idCard',
                        title: '身份证号'
                    }, {
                        field: 'address',
                        title: '操作',
                        formatter : function (value, row,index) {
                            return `
                       <a class="btn btn-default btn-sm" type="button"
                          data-toggle="modal"
                          onclick="merchantSystemAuthorityFun.authority_modify_array(this,'${row.userId}')">修改权限<span class="glyphicon glyphicon-pencil"></span></a>
                    `;
                        }
                    }
                ]
            });
        },
        //提交确认，关闭模态框
        subBizUserRole : function () {
            var treeObj = $.fn.zTree.getZTreeObj("systemAuthorityTree2");
            var nodes = treeObj.getCheckedNodes(true);//在提交表单之前将选中的checkbox收集
            var array = new Array();
            for(var i=0;i<nodes.length;i++){
                array.push(nodes[i].id);
            }
            //修改权限
            $.ajax({
                type: "POST",
                url: "user/updateByUserId?roleIdList="+array+"&userId="+$("#merchant-authorityIndexId").val(),
                dataType: "json",
                success: function(data){
                    layer.msg(data.message, {icon: 1});
                    $(".merchant-system-authority-edit-modal").modal('hide');

                    //修改成功，
                    //更新右侧树图的节点，，首先取消所有选中状态
                    var treeObj2 = $.fn.zTree.getZTreeObj("systemAuthorityTree");
                    treeObj2.checkAllNodes(false);
                    treeObj2.cancelSelectedNode();

                    //更新每个节点的信息
                    for(var i =0;i<nodes.length;i++){
                        var zTree2 = $.fn.zTree.getZTreeObj("systemAuthorityTree");
                        var node2 = zTree2.getNodeByParam("id",nodes[i].id);
                        node2.checked = true;
                        //设置选中之后，需要更新一次，否则无法选中该节点
                        zTree2.updateNode(node2);
                    }

                },
                error:function () {
                    alert("error");
                }
            });
        },
        /*获取事件对象*/
        getEvent : function(){
            return window.event || arguments.callee.caller.arguments[0];
        },
        //修改用户角色，打开模态框
        authority_modify_array : function (e,index) {
            //获取用户角色的userId
            $("#merchant-authorityIndexId").val(index);
            $(".merchant-system-authority-edit-modal").modal('show');
        }
    };
    merchantSystemAuthorityFun.init();
</script>
</body>
</html>